﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using SuppKids.Core;
using SuppKids.Core.Domain;
using SuppKids.Core.Extensions;
using SuppKids.Licensing.Common;

namespace SuppKids.Controls
{
    /// <summary>
    /// Interaction logic for Filter.xaml
    /// </summary>
    public partial class Filter : UserControl
    {
        public EventHandler<FilterEventArgs> OnFilterChanged;
        private FilterData filterData;

        public Filter()
        {
            InitializeComponent();
            
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                this.DayOfWeek.ItemsSource = EnumExtensions.GetPossibleValues<DayOfWeek>();
                this.WeekNumber.ItemsSource = EnumExtensions.GetPossibleValues<WeekNumber>();
                this.MealType.ItemsSource = EnumExtensions.GetPossibleValues<MealType>();
            }
        }

        public static readonly DependencyProperty MenuTypeProperty =
            DependencyProperty.Register(Lambda.Property<Filter>(a => a.MenuType), typeof (MenuType), typeof (Filter), new PropertyMetadata(default(MenuType)));

        public MenuType MenuType
        {
            get { return (MenuType) GetValue(MenuTypeProperty); }
            set { SetValue(MenuTypeProperty, value); }
        }

        public bool IsFilled
        {
            get
            {
                return this.FilterData != null;
            }
        }

        public FilterData FilterData
        {
            get { return filterData; }
        }


        private void MenuFilterChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.WeekNumber.SelectedItem == null ||
                this.DayOfWeek.SelectedItem == null ||
                this.MealType.SelectedItem == null)
            {
                return;
            }

            var weekNumber = (WeekNumber)this.WeekNumber.SelectedItem;
            var dayOfWeek = (DayOfWeek)this.DayOfWeek.SelectedItem;
            var menuType = this.MenuType;
            var mealType = (MealType)this.MealType.SelectedItem;

            this.filterData = new FilterData(weekNumber, dayOfWeek, menuType, mealType);

            if (this.OnFilterChanged != null)
            {
                this.OnFilterChanged(this, new FilterEventArgs(this.FilterData));
            }

        } 
        public class FilterEventArgs:EventArgs
        {
            private readonly FilterData filterData;

            public FilterData FilterData
            {
                get { return filterData; }
            }

            public FilterEventArgs(FilterData filterData)
            {
                this.filterData = filterData;
            }
        }
    }
    public class FilterData
    {
        private readonly WeekNumber weekNumber;
        private readonly DayOfWeek dayOfWeek;
        private readonly MenuType menuType;
        private readonly MealType mealType;

        public WeekNumber WeekNumber
        {
            get { return weekNumber; }
        }

        public DayOfWeek DayOfWeek
        {
            get { return dayOfWeek; }
        }

        public MenuType MenuType
        {
            get { return menuType; }
        }

        public MealType MealType
        {
            get { return mealType; }
        }

        public FilterData(WeekNumber weekNumber, DayOfWeek dayOfWeek, MenuType menuType, MealType mealType)
        {
            this.weekNumber = weekNumber;
            this.dayOfWeek = dayOfWeek;
            this.menuType = menuType;
            this.mealType = mealType;
        }
    }
}
